<!doctype html>

<html lang="zh-CN">

<head>
  <meta charset="utf-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover" />
  <meta http-equiv="X-UA-Compatible" content="ie=edge" />
  <link rel="shortcut icon" href="/favicon.ico">
  <title>消防管理系统 - 用户注册</title>
  <!-- 导入CSS -->
  <link href="./dist/css/tabler.min.css?1674944402" rel="stylesheet" />
  <link rel="stylesheet" href="./dist/css/register.css">
  <link rel="stylesheet" href="https://dongfangs.oss-cn-beijing.aliyuncs.com/forum/login/register.css">
  <link rel="stylesheet" href="./dist/css/jquery.toast.css">
</head>

<body class="d-flex flex-column">
  <!-- 正文 -->
  <div class="main">
    <div class="background">
      <div class="register-block">
        <a href="#" class="title">消防管理系统</a>
        <p class="register-title">用户注册</p>
      <form id="signUpForm" autocomplete="off" novalidate>
          <!-- 用户名 -->
          <div class="mb-3">
            <label class="form-label required">用户名</label>
            <input type="text" class="form-control " placeholder="请输入用户名" name="userAccount" id="userAccount">
            <div class="invalid-feedback">用户名不能为空</div>
          </div>
          <!-- 昵称 -->
          <div class="mb-3">
            <label class="form-label required">昵称</label>
            <input type="text" class="form-control" placeholder="请输入昵称" name="nickName" id="nickName">
            <div class="invalid-feedback">昵称不能为空</div>
          </div>
          <!-- 邮箱 -->
          <div class="mb-3">
            <label class="form-label required">邮箱</label>
            <input type="text" class="form-control" placeholder="请输入邮箱" name="email" id="email">
            <div class="invalid-feedback">邮箱不能为空</div>
          </div>
          <!-- 验证码 -->
          <div class="mb-3">
            <label class="form-label required">验证码</label>
            <input type="text" class="form-control" placeholder="请输入验证码" name="code" id="code">
            <div class="code-pane">
              <a class="down-btn tp-down-btn" data-sm-link-text="验证码" target="_blank" onclick="sendCode()"><span>获取</span></a>
            </div>
            <div class="invalid-feedback">验证码不能为空</div>
          </div>
          <!-- 密码 -->
          <div class="mb-3">
            <label class="form-label required">密码</label>
            <div class="input-group input-group-flat">
              <input type="password" class="form-control" placeholder="请输入密码" autocomplete="off" name="password"
                id="password">
              <span class="input-group-text">
                <a href="javascript:void(0);" class="link-secondary" id="password_a" title="显示密码"
                  data-bs-toggle="tooltip">
                  <svg xmlns="http://www.w3.org/2000/svg" class="icon" width="24" height="24" viewBox="0 0 24 24"
                    stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
                    <path stroke="none" d="M0 0h24v24H0z" fill="none" />
                    <path d="M12 12m-2 0a2 2 0 1 0 4 0a2 2 0 1 0 -4 0" />
                    <path
                      d="M22 12c-2.667 4.667 -6 7 -10 7s-7.333 -2.333 -10 -7c2.667 -4.667 6 -7 10 -7s7.333 2.333 10 7" />
                  </svg>
                </a>
              </span>
              <div class="invalid-feedback">密码不能为空</div>
            </div>
          </div>
          <div class="mb-3">
            <label class="form-check">
              <input type="checkbox" class="form-check-input" id="policy" />
              <span class="form-check-label">同意 <a href="#" tabindex="-1">消防管理系统使用条款和隐私政策</a>.</span>
            </label>
          </div>
          <div class="form-footer">
            <button type="button" class="btn btn-primary w-100" id="submit">注册</button>
          </div>
      </form>
        <div class="link">
          <a href="sign-in.html" class="login">登录账号</a>
        </div>
      </div>
    </div>
  </div>
</body>
  <!-- 导入JS -->
<script src="./dist/js/tabler.min.js"></script>
<script src="./dist/js/jquery-3.6.3.min.js"></script>
<script src="./dist/js/jquery.toast.js"></script>
<script src="js/common.js"></script>

<script>
  function sendCode() {
    $.ajax({
      type: "get",
      url: "/system/sysUser/code",
      contentType : 'application/x-www-form-urlencoded;charset=UTF-8',
      data: {
        email: $("#email").val()
      },
      success: function (result) {
        if (result.code == 1000){
          alert("验证码发送成功");
        }
        else {
          alert("邮箱格式错误");
        }
      }
    });
  }
</script>

<script>
// 当前页加载成功后执行的方法
$(function () {
  // 获取表单并校验
  $('#submit').click(function () {
    let checkForm = true;
    // 校验用户名
    if (!$('#userAccount').val()) {
      $('#userAccount').addClass('is-invalid');
      checkForm = false;
    }
    // 校验昵称
    if (!$('#nickName').val()) {
      $('#nickName').addClass('is-invalid');
      checkForm = false;
    }
    // 校验密码非空
    if (!$('#password').val()) {
      $('#password').addClass('is-invalid');
      checkForm = false;
    }
    // 校验邮箱非空
    if (!$('#email').val()) {
      $('#email').addClass('is-invalid');
      checkForm = false;
    }
    // 校验验证码非空
    if (!$('#code').val()) {
      $('#code').addClass('is-invalid');
      checkForm = false;
    }
    // 检验政策是否勾选
    if (!$('#policy').prop('checked')) {
      $('#policy').addClass('is-invalid');
      checkForm = false;
    }
    // 根据判断结果提交表单
    if (!checkForm) {
      return;
    }
    
    // 构造数据
    let postData = {
      userAccount : $('#userAccount').val(),
      nickName : $('#nickName').val(),
      password : $('#password').val(),
      email : $('#email').val(),
      code : $('#code').val(),
    };

    $.ajax ({
      type : 'post',
      url  : '/system/sysUser/register',
      contentType : 'application/x-www-form-urlencoded;charset=UTF-8',
      data : postData,
      // 回调方法
      success: function(respData){
        // 判断返回的状态码
        if (respData.code == 1000) {
          // 跳转到登录页面
          alert("注册成功");
          location.assign('/sign-in.html');
        } else {
          // 提示信息
          $.toast({
              heading: '警告',
              text: respData.message,
              icon: 'warning'
          });
        }
      }, 
      error : function() {
        // 提示信息
        $.toast({
              heading: '错误',
              text: '访问出现问题，请与管理员联系.',
              icon: 'error'
          });
      }
    });
  });

  // 表单元单独检验
  $('#userAccount, #nickName, #password, #email, #code').on('blur', function () {
    if ($(this).val()) {
      $(this).removeClass('is-invalid');
      $(this).addClass('is-valid');
    } else {
      $(this).removeClass('is-valid');
      $(this).addClass('is-invalid');
    }
  })

  // 校验政策是否勾选
  $('#policy').on('change', function () {
    if ($(this).prop('checked')) {
      $(this).removeClass('is-invalid');
      $(this).addClass('is-valid');
    } else {
      $(this).removeClass('is-valid');
      $(this).addClass('is-invalid');
    }
  })

  $('#password_a').click(function () {
    if($('#password').attr('type') == 'password') {
      $('#password').attr('type', 'text');
    } else {
      $('#password').attr('type', 'password');
    }
  });
});

</script>

</html>